package com.liangdemo.web.thread;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Name LiangErLe
 * @date 2022/6/9 18:01
 */
public class ThreadDemo4_5 {

    public static void main(String[] args) throws IOException {
        Boss boss = new Boss();
        Worker t1 = new Worker(boss, "线程1", 3, true);
        Worker t2 = new Worker(boss, "线程2", 4, true);
        Worker t3 = new Worker(boss, "线程3", 1, false);
        boss.addTask(t1);
        boss.addTask(t2);
        boss.addTask(t3);
        //启动线程
        boss.start();
        System.in.read();
    }


    private static class Boss extends Thread {

        private List<Worker> workers = new ArrayList<>();

        public void addTask(Worker t) {
            workers.add(t);
        }

        public void run() {
            workers.stream().forEach(e -> {
                e.start();
            });
        }

        public void end(Worker worker) {
//            if (worker.getResult() == Result.FATLED) {
//                System.exit(0);
//            }
            if (worker.getResult() == Result.FATLED) {
                cancel(worker);
            }
        }

        private void cancel(Worker worker) {
            for (Worker w : workers) {
                if (w != worker) w.cancel();

            }
        }


    }

    /**
     * 线程执行结果的集中状态
     * NOTEND；正在运行
     * SUCCESSED：成功
     * FATLED：失败
     * CANCELLED:取消
     */
    private static enum Result {
        NOTEND, SUCCESSED, FATLED, CANCELLED
    }


    private static class Worker extends Thread {

        private Result result = Result.NOTEND;
        private Boss boss;
        private String name;
        private int timeInSeconds;
        private boolean success;

        private boolean cancelling;

        public Worker(Boss boss, String name, int timeInSeconds, boolean success) {
            this.boss = boss;
            this.name = name;
            this.timeInSeconds = timeInSeconds;
            this.success = success;
        }

        public Result getResult() {
            return result;
        }

        public void run() {
//            try {
//                Thread.sleep(timeInSeconds * 1000);
//                System.out.println(name + "  任务结束");
//                result = success ? Result.SUCCESSED : Result.FATLED;
//                boss.end(this);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
            int interval = 100 * 10000;
            int total = 0;
            for (; ; ) {
                try {
                    Thread.sleep(interval);
                    total += interval;
                    if (total / 1000 >= timeInSeconds) {
                        System.out.println(name + " 任务结束" + result);
                        result = success ? Result.SUCCESSED : Result.FATLED;
                        break;
                    }
                    if (cancelling) {
                        rollback();
                        result = Result.CANCELLED;
                        cancelling = false;
                        System.out.println(name + " 任务结束" + result);
                        break;
                    }


                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            boss.end(this);
        }

        /**
         * 回滚
         */
        private void rollback() throws InterruptedException {
            //这里支持多种形式的事务回滚
            System.out.println(name + "rollback start");
            Thread.sleep(500);
            System.out.println(name + "rollback end");

        }

        public void cancel() {
            cancelling = true;
        }
    }
}
